package com.yips.controller;

import com.yips.domain.AjaxResult;
import com.yips.domain.entity.SysMenu;
import com.yips.domain.model.LoginUser;
import com.yips.service.SysMenuService;
import com.yips.service.web.LoginService;
import com.yips.util.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author h2tc01
 * @version 1.0.0
 * 2022/5/10 14:33
 */
@RestController
public class LoginController {

    @Autowired
    private LoginService loginService;

    @Autowired
    private SysMenuService menuService;


    @PostMapping("/login")
    public AjaxResult login(@RequestParam("username") String username, @RequestParam("password") String password, HttpSession session) {
        System.out.println("--------------------------------");
        AjaxResult ajax = AjaxResult.success();
        LoginUser loginUser = loginService.login(username, password);
        session.setAttribute("loginUser", loginUser);
        ajax.put("username", loginUser.getUser().getNickName());
        return ajax;
    }

    /**
     * 查询菜单树信息
     *
     * @return 查询菜单树信息
     */
    @GetMapping("/getMenuTree")
    public AjaxResult getMenuTree()
    {
        Long userId = SecurityUtils.getUserId();
        List<SysMenu> menus = menuService.selectMenuTreeByUserId(userId);
        return AjaxResult.success(menus);
    }
}
